package edu.hubu.javaweb01demo.view;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class Login extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String errMsg = (String) request.getAttribute("errMsg");

		out.println("<!DOCTYPE html>");
		out.println("<html lang='en'>");
		out.println("<head>");
		out.println("<meta charset='UTF-8'>");
		out.println("<meta http-equiv='X-UA-Compatible' content='IE=edge'>");
		out.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
		out.println("<title>Login</title>");
		out.println("<link rel='stylesheet' type='text/css' href='css/login.css'>");
		out.println("</head>");
		out.println("<body>");
		out.println("<div class='login-container'>");
		out.println("<h1>用户登录</h1>");

		// 登录表单
		out.println("<form action='/JavaWeb01demo/loginServlet' method='post'>");
		out.println("<div class='form-group'>");
		out.println("<label for='username'>账号：</label>");
		out.println("<input type='text' name='user' id='username' placeholder='请输入用户名' required>");
		out.println("</div>");
		out.println("<div class='form-group'>");
		out.println("<label for='password'>密码：</label>");
		out.println("<input type='password' name='pwd' id='password' placeholder='请输入密码' required>");
		out.println("</div>");
		out.println("<div class='form-group'>");
		out.println("<input type='submit' value='登录' class='btn-login'>");
		out.println("<input type='reset' value='重置' class='btn-reset'>");
		out.println("</div>");
		out.println("</form>");

		// 错误信息显示
		if (errMsg != null) {
			out.println("<div class='error-message'>" + errMsg + "</div>");
		}

		out.println("</div>");
		out.println("</body>");
		out.println("</html>");

		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}
}
